﻿<?xml version="1.0" encoding="utf-8"?><Articles><Article><ArticlesID>852</ArticlesID><Title>Sử dụng Session State trong HttpHandlers (ASHX files)</Title><Body>&amp;lt;p&amp;gt;Để sử dụng Session trong file ashx hoặc HttpHandlers bạn phải implement interface IReadOnlySessionState&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;C#&amp;lt;/p&amp;gt;
&amp;lt;pre class=&amp;quot;brush: csharp;&amp;quot; title=&amp;quot;code&amp;quot;&amp;gt;
&amp;amp;lt;% @ webhandler language=&amp;amp;quot;C#&amp;amp;quot; class=&amp;amp;quot;DownloadHandler&amp;amp;quot; %&amp;amp;gt;

using System;
using System.Web;
using System.Web.SessionState;

public class DownloadHandler : IHttpHandler, IReadOnlySessionState
{
   public bool IsReusable { get { return true; } }
  
   public void ProcessRequest(HttpContext ctx)
   {
       ctx.Response.Write(ctx.Session[&amp;amp;quot;fred&amp;amp;quot;]);
   }
}&amp;lt;/pre&amp;gt;
&amp;lt;p&amp;gt;VB.NET&amp;lt;/p&amp;gt;
&amp;lt;pre class=&amp;quot;brush: vb;&amp;quot; title=&amp;quot;code&amp;quot;&amp;gt;
Imports System.Web
Imports System.Web.Services
Imports System.Web.SessionState

Public Class test
    Implements System.Web.IHttpHandler, IReadOnlySessionState

    Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest

        context.Response.ContentType = &amp;amp;quot;text/plain&amp;amp;quot;
        context.Session(&amp;amp;quot;Test&amp;amp;quot;) = &amp;amp;quot;abc&amp;amp;quot;
        context.Response.Write(context.Session(&amp;amp;quot;Test&amp;amp;quot;))

    End Sub

    ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property

End Class&amp;lt;/pre&amp;gt;
&amp;lt;p&amp;gt;&amp;amp;nbsp;&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;&amp;amp;nbsp;&amp;lt;/p&amp;gt;
&amp;lt;p&amp;gt;&amp;amp;nbsp;&amp;lt;/p&amp;gt;</Body><CommentEnabled>True</CommentEnabled><ViewCount>0</ViewCount><ReleaseDate>3/18/2010 7:35:28 PM</ReleaseDate><TotalComment>0</TotalComment><IsMedia>False</IsMedia><IsPicture>False</IsPicture><IsVote>True</IsVote><ArticlePassword /><PostVisible>0</PostVisible><ArticlePath>/2010/3/18/Su-dung-Session-State-trong-HttpHandlers-ASHX-files-354</ArticlePath><Approved>True</Approved><ImagePath /><CategoryId>28</CategoryId><ArrayCatID>28|25</ArrayCatID></Article></Articles>